import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/layout/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('../views/Layout'),
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: Home
      },
      {
        path: 'about',
        name: 'About',
        component: () => import('../views/layout/About.vue'),
        children:[

        ]
      },
      {
        path: 'order',
        name: 'Order',
        component: () => import('../views/layout/Order')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login')
  },
  {
    path: '/user',
    name: 'UserEdit',
    component:()=>import('../views/user/UserEdit')
  },
  {
    path: '/address',
    name: 'AddressList',
    component:()=>import('../views/user/address/AddressList')
  },
  {
    path: '/address/edit',
    name:'AddressEdit',
    component:()=>import('../views/user/address/AddressEdit')
  },{
    path: '/shop',
    name:'ShopDetail',
    component:()=>import('../views/shop/ShopDetail')
  },{
    path: '/order/detail',
    name:'OrderDetail',
    component:()=>import('../views/order/OrderDetail')
  },

  {
    path: '/balance',
    name: 'Balance',
    component: () => import('../views/About/Balance/Balance')
  },
  {
    path: '/integral',
    name: 'Integral',
    component: () => import('../views/About/Integral/Integral')
  },
  {
    path: '/integral/description',
    name: 'IntegralDescription',
    component: () => import('../views/About/Integral/IntegralDescription')
  },
  {
    path: '/resetPassword',
    name: 'ResetPassword',
    component: () => import('../views/About/Reset/ResetPassword')
  },
  {
    path: '/service',
    name: 'Service',
    component: () => import('../views/About/service/Service')
  },
  {
    path: '/vip',
    name: 'Vip',
    component: () => import('../views/About/vip/Vip')
  }

]
/*
  服务中心
    Service
  会员
    Vip
  积分
      Integral
  积分说明
     IntegralDescription
  余额
  Balance
  重置密码
  ResetPassword


 */

const whiteList=['/','/login','/home','/order','/about']

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  router.app.$options.store.state.defaultTabbar = to.path.substr(1)
  console.log(to.path)
  if (whiteList.includes(to.path)) {
    // if (whiteList.indexOf(to.path) !== -1) {
    next()
  } else {
    if (Vue.$cookies.isKey('user_id')) {
      next()
    } else {
      next({ path: '/login' })
    }
  }
})

export default router
